package ${basePackage}.${service}.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
<#if haveBigDecimal>
import java.math.BigDecimal;
</#if>
<#if haveDate>
import java.util.Date;
</#if>
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import ${basePackage}.${entity}.${className}Entity;
import ${basePackage}.vo.${className}VO;
import ${basePackage}.pojo.Add${className}POJO;
import ${basePackage}.pojo.Update${className}POJO;
import ${basePackage}.${mapper}.${className}Mapper;
import ${basePackage}.${service}.I${className}Service;

/**
 * @描述: ${comment}
 * @作者: ${author}
 * @创建时间: ${createTime}
 * @所属公司: ${company}
 */
@Service
public class ${className}ServiceImpl extends ServiceImpl${r'<'}${className}Mapper, ${className}Entity> implements I${className}Service {

    @Autowired
    private ${className}Mapper m${className}Mapper;


    /**
     * 分页查询
     *
     * @param pageNum   分页参数
     * @param pageSize  分页参数
     * @param data      其他查询过滤参数
     * @return 分页列表数据
     */
    @Override
    public PageInfo${r'<'}${className}VO> pageList(int pageNum, int pageSize, ${className}VO data) {
        pageNum = Math.max(pageNum, 1);
        pageSize = pageSize < 1 || pageSize > 50 ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        List${r'<'}${className}VO> results = m${className}Mapper.select(data);

        PageInfo${r'<'}${className}VO> pageInfo = new PageInfo${r'<'}>(results);

        return pageInfo;
    }

    /**
     * 查询一条数据
     *
    <#list pkColumns as column>
     * @param ${column.attrName} ${column.columnComment}
    </#list>
     * @return 一条数据
     **/
    @Override
    public ${className}VO getOneByPk(<#list pkColumns as column>${column.attrType} ${column.attrName}<#if column_has_next>, </#if></#list>) {
        ${className}VO result = m${className}Mapper.selectByPk(<#list pkColumns as column>${column.attrName}<#if column_has_next>, </#if></#list>);

        return result;
    }

    /**
     * 新增数据
     *
     * @param data         数据
     * @return 是否成功(false-失败，true-成功)
     **/
    @Override
    public boolean add(Add${className}POJO data) {
        ${className}Entity param = new ${className}Entity();
        <#list columns as column>
            <#if (pkColumns?size == 1 && column.columnKey == "PRI")>
        param.set${column.upcaseAttrName}(GenerateCodeUtils.generateId());
            <#elseif (column.attrName == "createBy" || column.attrName =="createUser" || column.attrName == "updateBy" || column.attrName == "updateUser" || column.attrName == "createAt" || column.attrName == "createTime" || column.attrName == "createDate" || column.attrName == "updateAt" || column.attrName == "updateTime" || column.attrName == "updateDate" || column.attrName == "valid")>
            <#else>
        param.set${column.upcaseAttrName}(data.get${column.upcaseAttrName}());
            </#if>
        </#list>

        return super.save(param);
    }

    /**
     * 批量新增数据
     *
     * @param datas        数据
     * @return 是否成功(false-失败，true-成功)
     **/
    @Override
    public boolean addBatch(List${r'<'}Add${className}POJO> datas) {
        List${r'<'}${className}Entity> params = new ArrayList<>();
        ${className}Entity param;
        for(Add${className}POJO data : datas) {
            param = new ${className}Entity();
        <#list columns as column>
            <#if (pkColumns?size == 1 && column.columnKey == "PRI")>
            param.set${column.upcaseAttrName}(GenerateCodeUtils.generateId());
            <#elseif (column.attrName == "createBy" || column.attrName =="createUser" || column.attrName == "updateBy" || column.attrName == "updateUser" || column.attrName == "createAt" || column.attrName == "createTime" || column.attrName == "createDate" || column.attrName == "updateAt" || column.attrName == "updateTime" || column.attrName == "updateDate" || column.attrName == "valid")>
            <#else>
            param.set${column.upcaseAttrName}(data.get${column.upcaseAttrName}());
            </#if>
        </#list>
            params.add(param);
        }

        return super.saveBatch(params);
    }

    /**
     * 更新数据
     *
     * @param data         待更新数据
     * @return 是否成功(false-失败，true-成功)
     **/
    @Override
    public boolean modify(Update${className}POJO data) {
        <#list pkColumns as column>
        if (data.get${column.upcaseAttrName}() == null) {
            return false;
        }
        </#list>
        ${className}Entity param = new ${className}Entity();
        <#list columns as column>
            <#if (column.attrName == "createBy" || column.attrName =="createUser" || column.attrName == "updateBy" || column.attrName == "updateUser" || column.attrName == "createAt" || column.attrName == "createTime" || column.attrName == "createDate" || column.attrName == "updateAt" || column.attrName == "updateTime" || column.attrName == "updateDate" || column.attrName == "valid")>
            <#else>
        param.set${column.upcaseAttrName}(data.get${column.upcaseAttrName}());
            </#if>
        </#list>

        return super.updateById(param);
    }

    /**
     * 移除一条数据(mybatisplus不支持多主键删除，所以用下面的方法来支持逻辑删除功能)
     *
    <#list pkColumns as column>
     * @param ${column.attrName} ${column.columnComment}
    </#list>
     * @param actionUserId 操作用户ID
     * @return 是否成功(false-失败，true-成功)
     **/
    @Override
    public boolean removeOne(<#list pkColumns as column>${column.attrType} ${column.attrName}, </#list>String actionUserId) {
        int result = m${className}Mapper.deleteLogic(<#list pkColumns as column>${column.attrName}, </#list>actionUserId);

        return result == 1;
    }
}
